<template>
  <el-dialog
    :close-on-click-modal="false"
    :close-on-press-escape="false"
    :title="!form.id ? $t('add') : $t('update')"
    :visible.sync="visible"
    width="85vw"
  >
    <el-form
      :model="form"
      :rules="dataRule"
      label-width="120px"
      ref="dataForm"
      v-loading="loading"
    >

      <el-form-item
        label="产品品类"
        prop="productTypeId">

        <product-type-select
          :type="form.productTypeId"
          @change="onTypeChanged"/>
      </el-form-item>

      <el-form-item
        label="用例名称"
        prop="taskName">
        <!--                        :placeholder="$t('model.name')"
                                    -->
        <el-input
          placeholder="用例名称"
          v-model="form.taskName"
          size="normal"
        />
      </el-form-item>

      <el-form-item
        label="内容"
        v-model="form.taskContent"
        prop="taskContent"
      >
        <MonacoEditor
          class="code-editor-container"
          v-model="form.taskContent"
          language="json"
          line-numbers="false"
          theme="vs-dark"
        />

      </el-form-item>
    </el-form>
    <template slot="footer">
      <el-button
        :loading="requesting"
        @click="visible = false">{{
          $t("cancel")
        }}</el-button>
      <el-button
        :loading="requesting"
        @click="dataFormSubmitHandle()"
        type="primary"
      >{{ $t("confirm") }}
      </el-button>
    </template>
  </el-dialog>
</template>

<script>
import MonacoEditor from 'vue-monaco'
import ProductTypeSelect from '../../../components/product.type.select'
export default {
  name: 'TestCaseAddOrUpdate',
  components: { ProductTypeSelect, MonacoEditor },
  data () {
    return {
      visible: false,
      loading: false,
      requesting: false,
      producttypes: [],
      form: {
        id: '',
        productTypeId: '',
        taskContent:
          '{\n' +
                '\t"workStatus": "",\n' +
                '\t"workMode": null,\n' +
                '\t"workHour": "0",\n' +
                '\t"workMinute": "0",\n' +
                '\t"workSecond": "0",\n' +
                '\t"weightUnitSet": null,\n' +
                '\t"temperatureUnitSet": null,\n' +
                '\t"hourSet": null,\n' +
                '\t"minuteSet": null,\n' +
                '\t"secondSet": null,\n' +
                '\t"tempSet": null,\n' +
                '\t"steamQuantitySet": null,\n' +
                '\t"firePowerSet": null,\n' +
                '\t"probeTempSet": null,\n' +
                '\t"menuId": "0",\n' +
                '\t"totalStep": "1",\n' +
                '\t"stepNum": "1",\n' +
                '\t"preHeat": null,\n' +
                '\t"temperatureUnit": null,\n' +
                '\t"weightUnit": null,\n' +
                '\t"temperature": "0",\n' +
                '\t"probeTemperature": "0",\n' +
                '\t"firePower": null,\n' +
                '\t"steamQuantity": null,\n' +
                '\t"weight": null,\n' +
                '\t"peopleNumber": null,\n' +
                '\t"cookings": null,\n' +
                '\t"lock": null,\n' +
                '\t"furnaceLight": null,\n' +
                '\t"power": null\n' +
                '}',
        taskName: ''
      }
    }
  },
  computed: {
    dataRule () {
      return {
        taskName: [
          {
            required: true,
            message: this.$t('validate.required'),
            trigger: 'blur'
          }
        ],
        taskContent: [
          {
            required: true,
            message: this.$t('validate.required'),
            trigger: 'blur'
          }
        ],
        productTypeId: [
          {
            required: true,
            message: this.$t('validate.required'),
            trigger: 'blur'
          }
        ]

      }
    }
  },
  mounted () {
    this.loadProductType()
  },
  methods: {
    init () {
      this.visible = true

      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields()

        if (this.form.id) {
          this.getInfo()
        }
      })
    },
    // 获取信息
    getInfo () {
      this.loading = true
      this.$http
        .get(`/product/ptestcase/${this.form.id}`)
        .then(({ data: res }) => {
          if (res.code !== 0) {
            return this.$message.error(res.msg)
          }

          this.form = {
            ...this.form,
            ...res.data
          }
        })
        .catch(() => {})
        .finally(() => {
          this.loading = false
        })
    },
    loadProductType () {
      this.$http
        .get('/product/pproductinfo/producttypelist', {
          params: { page: 1, limit: 100 }
        })
        .then(res => {
          this.producttypes = res.data.data
        })
    },

    onTypeChanged (value) {
      // console.log(value)
      this.form.productTypeId = value
    },
    dataFormSubmitHandle () {
      this.$refs['dataForm'].validate(valid => {
        if (!valid) {
          return false
        }
        if (this.requesting) {
          return
        }
        let params = {
          ...this.form
          // status: this.form.status ? 1 : 0
        }

        this.requesting = true
        this.$http[!this.form.id ? 'post' : 'put'](
          '/product/ptestcase',
          params
        )
          .then(({ data: res }) => {
            if (res.code !== 0) {
              return this.$message.error(res.msg)
            }
            this.$message({
              message: this.$t('prompt.success'),
              type: 'success',
              duration: 500,
              onClose: () => {
                this.visible = false
                this.$emit('refreshDataList')
              }
            })
          })
          .catch(() => {})
          .finally(() => {
            this.requesting = false
          })
      })
    }
  }
}
</script>

<style scoped lang="scss">
.code-editor-container {
  height: 40vh;
}
</style>
